package com.jerry.weixing.util;

import com.jerry.weixing.domain.*;
import com.jerry.weixing.servlet.WeixingServlet;
import com.thoughtworks.xstream.XStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description  把对象转换成xml或者把xml转化成对象
 * @author Jerry
 * @date 2016-10-09
 */
public class TransFromUtil {

    private static final  String localUrl = "http://myweixing.tunnel.2bdata.com/WeXin/";

    /**
     * 把微信前台传过来的xml转化成map
     * @param req
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public static Map<String,Object> transXmlToMap(HttpServletRequest req) throws IOException, DocumentException {
        SAXReader reader = new SAXReader();
        InputStream ins = req.getInputStream();
        Map<String,Object> map = new HashMap<>();

        Document doc = reader.read(ins);
        Element em = doc.getRootElement();
        List<Element> list = em.elements();

        for(Element e :list){
            map.put(e.getName(),e.getText());
        }
        ins.close();
        return map;
    }

    /**
     * 把自己后台的消息封装成xml发送到前台
     * @param message
     * @return
     */
    public  static String transMessageToXml(Message message){
        XStream xstream = new XStream();
        xstream.alias("xml",message.getClass());
        return xstream.toXML(message);
    }

    public static String initImage(String ToUserName,String FromUserName){
        ImageMessage imageMessage = new ImageMessage();
        imageMessage.setFromUserName(ToUserName);
        imageMessage.setToUserName(FromUserName);
        imageMessage.setCreateTime(new Date().getTime());
        imageMessage.setMsgType("image");

        Image image = new Image();
        image.setMediaId("A6m0EqwXfDdqvgdX8e4rB6YuEuoIjF9lAyM2aQCnZPXbtWgs5TcX9otWFMfbHggY");

        imageMessage.setImage(image);
        return transMessageToXml(imageMessage);

    }

    public static String initMusic(String ToUserName,String FromUserName){
        MusicMessage musicMesage = new MusicMessage();
        musicMesage.setFromUserName(ToUserName);
        musicMesage.setToUserName(FromUserName);
        musicMesage.setCreateTime(new Date().getTime());
        musicMesage.setMsgType("music");

        Music music = new Music();
        music.setTitle("阿瑟小子");
        music.setDescription("useher 嘻哈");
        music.setMusicUrl(localUrl+"music/Usher.mp3");
        music.setHQMusicUrl(localUrl+"music/Usher.mp3");
        music.setThumbMediaId("XcbzTAmnAwZSYMWJlSI8yTMWnJaKEy88JsqAyhrhMwlEfkrHSyMuYFsnaUurm0VD");

        musicMesage.setMusic(music);
        return transMessageToXml(musicMesage);

    }

    public static  String tranNewsToXml(NewsMessage message){
        XStream xstream = new XStream();
        xstream.alias("xml",message.getClass());
        xstream.alias("item",new News().getClass());
        return xstream.toXML(message);
    }

    /**
     * 订阅时应该回复给客户的消息
     * @param ToUserName
     * @param FromUserName
     * @return
     */
    public static String init(String ToUserName,String FromUserName){
        SubscribeMessage message = new SubscribeMessage();
        message.setCreateTime(new Date().getTime());
        message.setFromUserName(ToUserName);
        message.setToUserName(FromUserName);
        message.setMsgType("text");
        message.setContent(initMenu());
        return transMessageToXml(message);
    }

    /**
     * 发送文本消息
     * @param ToUserName
     * @param FromUserName
     * @param content
     * @return
     */
    public static String initContent(String ToUserName,String FromUserName,String content){
        Message message = new Message();
        message.setCreateTime(new Date().getTime());
        message.setFromUserName(ToUserName);
        message.setToUserName(FromUserName);
        message.setMsgType("text");
        message.setContent(content);
        return transMessageToXml(message);
    }

    /**
     * 订阅时间过后对用户发送的消息
     * @return
     */
    private  static  String initMenu(){
        StringBuffer sb = new StringBuffer();
        sb.append("你好，欢迎你关注本订阅号\n\n");
        sb.append(" 想了解更多请回复1\n\n");
        sb.append(" 想知道更少请回复2\n\n");
        return sb.toString();
    }



}
